import urllib
import json
import re

def existsInWikipedia(term):
    url = 'http://en.wikipedia.org/w/api.php?action=opensearch&search='+term+'&limit=15&format=json'
    search_response = urllib.urlopen(url)
    content = search_response.read()
    search_results = json.loads(content)
    if len(search_results[1]) == 0:
        return False
    else:
        print(search_results[1])
        
print "hello"
query = raw_input('What is your query?')
params = urllib.urlencode({'q':query, 'f':'json', 'i':'false', 'length':200, 'src':'web'})
f = urllib.urlopen("http://www.faroo.com/api?" + params)
msg = f.read()
result = json.loads(msg)
for item in result.get('results'):
#    print item.get('url')
    if re.search('wikipedia', item.get('url'),re.IGNORECASE) is not None:
        print item.get('title')
    if re.search('money', item.get("kwic"), re.IGNORECASE) is not None:
        print item.get('author') + " should donate money"
    else:
        print item.get('kwic')

        


